clear all
set more off, perm
global cd "/mnt/brownresearch/ECON_LaPorta/rlaporta/research/BGLS2023"
global Tables $cd/XS/Tables
global Data   $cd/XS/DataForTables
* *******************************************
set varabbrev off
* *******************************************
/*
use datem LTG STGE1 cay spc kellypruitt_mrp PVS bloom_uncertainty gz_spr HighYieldShare svix12 STGE1 CAPE dp e_cae3 using $Data/TimeSeries01, clear

* created by FF01.do 
merge 1:1 datem using $Work/FF01
keep if _merge==3
drop _merge 

gen LTGmean=LTG*100
*/
* ***********************************
use datem STGE1 LTG spc cay e_cae3 CAPE using /srv/update/Table3, clear
merge 1:1 datem using /srv/update/Table02
drop _merge

* created by FF01.do 
merge 1:1 datem using /srv/update/FF01
keep if _merge==3
drop _merge MKTMo1 MKTYr*

gen LTGmean=LTG*100

local time "Mo1 Yr1 Yr3 Yr5"
foreach t of local time {
	generate MKT`t'=MKTRF`t'+RF`t'
}
drop MKTRF*
su MKT*
* ***********************************
* Take logs:
foreach var of varlist SMB* HML* RMW* CMA* BAB* {
	generate ln`var'=ln(1+`var'/100)
}
gen ltghi3 =ln(1+LTGHiYr3/100)
gen ltghi5 =ln(1+LTGHiYr5/100)

gen ltglo3 =ln(1+LTGLoYr3/100)
gen ltglo5 =ln(1+LTGLoYr5/100)

gen ltglmh3=ltglo3-ltghi3
gen lnPMOYr5=ltglo5-ltghi5

gen mkt3=ln(1+MKTYr3/100)
gen mkt5=ln(1+MKTYr5/100)
gen inter=mkt5*cay

keep if dofm(datem)< mdy(1,1,2016)
keep if LTG<.

* cap outliers (all variables except returns)
foreach var of varlist LTG cay inter spc kellypruitt_mrp PVS bloom_uncertainty gz_spr HighYieldShare svix12 STGE1 CAPE dp e_cae3 {
	quietly: egen max=pctile(`var'), p(99)
	quietly: egen min=pctile(`var'), p(1)  
	quietly: replace `var'=max if `var'>max & `var'<.
	quietly: replace `var'=min if `var'<min	
	drop max min 
}
* set mean to zero and stddev to 1
foreach var of varlist LTG cay inter spc kellypruitt_mrp PVS bloom_uncertainty gz_spr HighYieldShare svix12 ltg* mkt* CAPE dp e_cae3 lnHMLYr* lnRMWYr* lnCMAYr* lnSMBYr* {
	egen `var'std=std(`var')
	replace `var'=`var'std
	drop `var'std
}
* ****************************************************** 
* Table 7
* ****************************************************** 
tsset datem 
gen L1LTG=L12.LTG
gen d1LTG=LTG-L1LTG
gen pd=-dp

eststo clear
eststo: quietly: ivreg2  ltglo5  L1LTG d1LTG mkt5 if mdy<mdy(1,1,2016), bw(61) robust	
eststo: quietly: ivreg2  ltghi5  L1LTG d1LTG mkt5 if mdy<mdy(1,1,2016), bw(61) robust	
eststo: quietly: ivreg2  lnPMOYr5 L1LTG d1LTG mkt5 if mdy<mdy(1,1,2016), bw(61) robust	

foreach var of varlist spc cay svix12 pd {
generate X=`var'
eststo: quietly: ivreg2  lnPMOYr5 L1LTG d1LTG mkt5 X if mdy<mdy(1,1,2016), bw(61) robust	
drop X
}
esttab				,  nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) label nogap stats(N r2_a) order(d*LTG) drop(_cons)
esttab  using $Tables/Table7.csv,  nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) label nogap stats(N r2_a) order(d*LTG) drop(_cons) replace
* *********************************************************
* Table 9  
* *********************************************************
eststo clear
foreach Y of varlist lnHMLYr5 lnRMWYr5 lnCMAYr5 lnSMBYr5  {
	quietly: eststo regYr5_`Y': ivreg2 `Y'	d1LTG	L1LTG	mkt5, bw(61) robust  
}	
esttab 					, nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel stats(N r2_a) nogap replace
esttab regYr5_*	using $Tables/Table9.csv, nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel stats(N r2_a) nogap replace
* ****************************************************** 
* Appendix C1 (exact match)
* ****************************************************** 
eststo clear
eststo: quietly: ivreg2  ltglo5   L1LTG d1LTG mkt5 pd if mdy<mdy(1,1,2016), bw(61) robust	
eststo: quietly: ivreg2  ltghi5   L1LTG d1LTG mkt5 pd if mdy<mdy(1,1,2016), bw(61) robust	
eststo: quietly: ivreg2  lnPMOYr5 L1LTG d1LTG mkt5 pd if mdy<mdy(1,1,2016), bw(61) robust	
foreach var of varlist spc cay svix12 {
generate X=`var'
eststo: quietly: ivreg2  lnPMOYr5 L1LTG d1LTG mkt5 pd X if mdy<mdy(1,1,2016), bw(61) robust	
drop X
}
esttab 					,  nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) label nogap stats(N r2_a) order(d*LTG)
esttab using $Tables/AppendixC1.csv	,  nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) label nogap stats(N r2_a) drop(_cons) order(d*LTG) replace
* *********************************************************
* Appendix C2.A
* *********************************************************
eststo clear
eststo: quietly: ivreg2  ltglo3  L1LTG d1LTG mkt3 	if mdy<mdy(1,1,2016), bw(61) robust	
eststo: quietly: ivreg2  ltghi3  L1LTG d1LTG mkt3 	if mdy<mdy(1,1,2016), bw(61) robust	
eststo: quietly: ivreg2  ltglmh3 L1LTG d1LTG mkt3 	if mdy<mdy(1,1,2016), bw(61) robust	

esttab 					,  nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) label nogap stats(N r2_a) order(d*LTG)
esttab using $Tables/AppendixC2.csv	,  nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) label nogap stats(N r2_a) order(d*LTG) drop(_cons) replace
* *********************************************************
* Appendix C4.A 
* *********************************************************
eststo clear
foreach Y of varlist lnHMLYr3 lnRMWYr3 lnCMAYr3 lnSMBYr3 {
	eststo: quietly: ivreg2  `Y' L1LTG d1LTG mkt3 if mdy<mdy(1,1,2016), bw(37) robust	
}
esttab 	,  nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) label nogap stats(N r2_a) order(d*LTG)
esttab  using $Tables/AppendixC4.csv,  nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) label nogap stats(N r2_a) order(d*LTG) drop(_cons) replace
* *********************************************************
* Appendix C5 
* *********************************************************
eststo clear
foreach Y of varlist lnHMLYr5 lnRMWYr5 lnCMAYr5 lnSMBYr5 lnBABYr5   {
	quietly: eststo: ivreg2 `Y'	d1LTG	L1LTG	mkt5 cay inter, bw(61) robust  
}	
esttab 					, nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel stats(N r2_a) nogap 
esttab  using $Tables/AppendixC5.csv	, nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01)   label stats(N r2_a) nogap order(d*LTG) drop(_cons) replace
* **************************************************************
* Appendix C6 -- Ret for PMO was computed in a different way than in rest of tables (without monthly rebalancing)
* **************************************************************
eststo clear
foreach Y of varlist lnPMOYr5 lnHMLYr5 lnRMWYr5 lnCMAYr5 lnSMBYr5  {
	quietly: regress `Y'  d1LTG L1LTG if dofm(datem)<mdy(1,1,2016), robust
	local rsqr=e(r2_a)	

	quietly: eststo: ivreg2 `Y' mkt5 if dofm(datem)<mdy(1,1,2016) & d1LTG+L1LTG<., bw(61) robust  
	quietly estadd scalar ar2=`rsqr'
}	
esttab					,  nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) label nogap stats(N r2_a ar2) drop(_cons) 
esttab  using $Tables/AppendixC6.csv	,  nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) label nogap stats(N r2_a ar2) drop(_cons) replace
* **************************************************************
* Appendix D1.B
* **************************************************************
eststo clear
*
foreach Y of varlist  lnPMOYr5 lnHMLYr5 lnRMWYr5 lnCMAYr5 lnSMBYr5  {
	quietly: eststo reg1_`Y': ivreg2 `Y'	d1LTG	L1LTG	mkt5 e_cae3 if mdy<mdy(1,1,2016), bw(61) robust  
}	
esttab					,  nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) label stats(N r2_a) order(d*LTG) nogap drop(_cons) 
esttab  using $Tables/AppendixD1B.csv	,  nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) label stats(N r2_a) order(d*LTG) nogap drop(_cons) replace
* **************************************************************
* **************************************************************
* FORECAST ERRORS (FROM XSection03.do)
* **************************************************************
* **************************************************************
use if (LTG+bookeq<.) & (bookeq>0) using $Work/FamaFrench05bb, clear
* add returns
merge 1:1 permco permno datem using $Work/FamaFrench02Ret, keepusing (permco permno datem mcap0-mcap60 ret1-ret60)
keep if _merge==3
drop _merge
* add earnings (earnings.dta generated by earnings.do)
merge 1:1 permco permno datem using $Work/earnings
drop if _merge==2
drop _merge
keep if SIZE>=1 & SIZE<=10
* size portfolios will be based on above/below median  
quietly: generate B=ceil(SIZE/5)
drop SIZE
/* **************************************************************** */
tempfile tmp
save "`tmp'", replace
/* **************************************************************** */
* do calculations for Fama French portfolios (will work with 3x2 portfolios)
foreach RNK in BOOK PROF INVE { 
use if `RNK'<. using "`tmp'", clear
quietly: generate H=1 if `RNK'>=01 & `RNK'<=03
quietly: replace  H=3 if `RNK'>=04 & `RNK'<=07
quietly: replace  H=5 if `RNK'>=08 & `RNK'<=10
drop `RNK'
save $Work/tmp`RNK', replace
* *********************************
* Compute portfolio-level variables for portfolio that is rebalanced monthly
preserve
forvalues x=1(1)60 {
	local j=`x'-1
	bys H B datem: egen TM=total(mcap`j' * (!missing(ret`x')))  
	bys H B datem: gen vwret`x'=ret`x'*(mcap`j'/TM)
	drop TM
	quietly: gen EP`x'=F`x'street if mcap`x'<.
}
collapse (sum) mcap* vwret* EP*, by(H B datem)
forvalues x=1(1)60 {
	quietly: replace  mcap`x'=. if  mcap`x'==0
	quietly: replace vwret`x'=. if vwret`x'==0
	quietly: replace    EP`x'=. if    EP`x'==0
}
gen p=10*H+B
tsset p datem
drop p

save $Work/portf, replace
restore
/* ********************************* */
keep permco permno datem H B streetLTM*
*#2) Work with firms that get delisted 
*#2.a) add data on returns, earnings and capitalization of FF portfolios
merge m:1 H B datem using $Work/portf
drop _merge

*#2.b) find data on distributions when delisted
merge m:1 permco permno using $Work/Delistings02
keep if _merge==3
drop _merge
order permco permno datem dlstmo H B dlstcd distrib 

*#2.c) WORK with relevant obs 
* Firm is delisted --> investor earns return on rebalanced portfolio --> original investment is assigned fractional earnings of rebalanced portfolio.
gen T=dlstmo-datem
keep if T>0 & T<61

su T, d
gen capital0=.
forvalues f=1(1)60 {
	local i=`f'-1
	quietly: generate capital`f'=distrib if `f'==T
	quietly: replace  capital`f'=capital`i'*(1+vwret`f') if `f'>T
	quietly: generate  E`f'=(capital`f'/mcap`f')*EP`f'
}
* keep data on earnings in yearly intervals
forvalues x=1(1)5 {
	local t=12*`x'
	quietly: gen REINV`x'=E`t'
}
keep permco permno datem dlstmo  REINV1 REINV2 REINV3 REINV4 REINV5
order permco permno datem dlstmo REINV1 REINV2 REINV3 REINV4 REINV5

merge 1:1 permco permno datem using $Work/tmp`RNK'
drop if _merge==1
drop _merge
* ********** 
* forecasts (use medest_t when available; else proxy medest_t based on LTG and medest_t-1)
generate f1=medest1*shs0/1000	if nsecurities==1 
generate f2=medest2*shs0/1000	if nsecurities==1	 
generate f3=medest3*shs0/1000	if nsecurities==1
generate f4=medest4*shs0/1000	if nsecurities==1	 
generate f5=medest5*shs0/1000	if nsecurities==1	 

replace f2=(1+LTG/100)*f1 if LTG>-100 & f1>0 & f2==.
replace f3=(1+LTG/100)*f2 if LTG>-100 & f2>0 & f3==.
replace f4=(1+LTG/100)*f3 if LTG>-100 & f3>0 & f4==.
replace f5=(1+LTG/100)*f4 if LTG>-100 & f4>0 & f5==.

* earnings 
generate e1=streetLTM1*(shs0/shs1)
generate e2=streetLTM2*(shs0/shs2)
generate e3=streetLTM3*(shs0/shs3)
generate e4=streetLTM4*(shs0/shs4)
generate e5=streetLTM5*(shs0/shs5)
* when firm is delisted (dlstmo>=datem), assing earnins from rebalanced portfolio
forvalues x=1(2)5 {
	generate new_e`x'=e`x'
	replace  new_e`x'=REINV`x'	if e`x'==. & (dlstmo>=datem)
	drop e`x'
}
tsset permco datem
generate nume1=new_e1 		if f1+new_e1<.
generate base1=f1 	 	if f1+new_e1<.

generate nume3=new_e3	 	if LTG+new_e3+L3.streetLTM<.
generate base3=L3.streetLTM 	if LTG+new_e3+L3.streetLTM<.

generate nume5=new_e5 		if LTG+new_e5+L3.streetLTM<.
generate base5=L3.streetLTM 	if LTG+new_e5+L3.streetLTM<.

gen ege1=ln(nume1)/1-ln(base1)/1
gen ege3=ln(nume3)/3-ln(base3)/3 - LTG/100
gen ege5=ln(nume5)/5-ln(base5)/5 - LTG/100

foreach x of varlist ege1 ege3 ege5 {
	quietly: bys datem: egen max=pctile(`x'), p(99)
	quietly: bys datem: egen min=pctile(`x'), p(1)  
	quietly: replace `x'=max if `x'>max & `x'<.
	quietly: replace `x'=min if `x'<min	
	drop max min
}	
collapse (mean) ege1 ege3 ege5, by(datem H B)
foreach Z in ege1 ege3 ege5  {
	replace `Z'=. if `Z'==0
	}

keep if dofm(datem)<mdy(1,1,2016)
foreach x of varlist ege1 ege3 ege5  {
	quietly: bys H B: egen max=pctile(`x'), p(99)
	quietly: bys H B: egen min=pctile(`x'), p(1)  
	quietly: replace `x'=max if `x'>max & `x'<.
	quietly: replace `x'=min if `x'<min	
	drop max min 
}
save $Work/errors_`RNK', replace
}
********************************
********************************
* Errors for BOOK PROF INVE
foreach RNK in BOOK PROF INVE { 
use $Work/errors_`RNK', clear
reshape wide ege1 ege3 ege5, i(datem B) j(H)
********
* High minus low:
gen HML1=ege15-ege11
gen HML3=ege35-ege31
gen HML5=ege55-ege51

*average HML for BIG and SMALL (as in Fama French)
collapse (mean) HML*, by(datem)
foreach var of varlist HML* {
	replace `var'=. if `var'==0
}
*add aggregate LTG
merge 1:1 datem using /srv/Feb2023/TimeSeries01, keepusing(datem LTG)
keep if _merge==3 & LTG<.
drop _merge
keep if dofm(datem)<mdy(1,1,2016)
foreach var of varlist HML* LTG  {
	egen tmp=std(`var')
	replace `var'=tmp
	drop tmp
}
tsset datem
gen L1LTG=L12.LTG
gen d1LTG=LTG-L1LTG
keep if d1LTG<.

display "`RNK'"
local Z "d1LTG L1LTG"
forvalues x=1(2)5 {
	replace   HML`x'=  -HML`x' 	if "`RNK'"=="INVE"

	local lag=12*`x'
	quietly:  eststo su_`RNK'_`x': ivreg2 HML`x' 	`Z', robust bw(`lag')
	}
}
esttab su*_3, nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nogap   drop(_cons)	stats(N r2_a) 
esttab su*_5, nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nogap   drop(_cons)	stats(N r2_a) 

esttab su*_3 using $Tables/AppendixC4.csv, nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nogap drop(_cons) stats(N r2_a) append
esttab su*_5 using $Tables/Table9.csv	 , nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nogap drop(_cons) stats(N r2_a) replace
* *************************************
* do SMB portfolio (average )
eststo clear
foreach RNK in BOOK PROF INVE { 
use $Work/errors_`RNK', clear
keep if H+B<.

*Average FEs across BIG and SMALL for High, Neutral and Low portfolios
reshape wide ege1 ege3 ege5, i(datem H) j(B)
collapse (mean) ege1* ege3* ege5*, by(datem)
order datem ege*  
foreach var of varlist ege11 ege12 ege31 ege32 ege51 ege52  {
	replace `var'=. if `var'==0
}
gen SMB1_`RNK'= (ege11 - ege12)
gen SMB3_`RNK'= (ege31 - ege32)
gen SMB5_`RNK'= (ege51 - ege52)

keep datem SMB* 
save $Work/SMB_`RNK', replace
}
********
* SMB Fama-French factor is based on mean SMB error across BOOK, CMA, CMA, RMW  
use $Work/SMB_BOOK,  clear
merge 1:1 datem using $Work/SMB_PROF
drop _merge
merge 1:1 datem using $Work/SMB_INVE
drop _merge
* CMA factor is based on Low minus High Investment, switch sign for SMB_INVE 
gen SMB1=(SMB1_BOOK+SMB1_INVE+SMB1_PROF)/3
gen SMB3=(SMB3_BOOK+SMB3_INVE+SMB3_PROF)/3
gen SMB5=(SMB5_BOOK+SMB5_INVE+SMB5_PROF)/3
keep datem SMB1 SMB3 SMB5 
********
merge 1:1 datem using /srv/Feb2023/TimeSeries01, keepusing(datem LTG)
keep if _merge==3 & LTG<.
drop _merge

tsset datem
keep if dofm(datem)<mdy(1,1,2016)

foreach var of varlist SMB* LTG   {
	egen tmp =std(`var')
	replace `var'=tmp
	drop tmp
}
gen L1LTG=L12.LTG
gen d1LTG=LTG-L1LTG
keep if d1LTG<.

eststo clear
local Z "d1LTG L1LTG"
forvalues x=1(2)5 {
	local lag=12*`x'
	quietly:  eststo su_`x': ivreg2   SMB`x' `Z', robust bw(`lag')
}
esttab su_3, nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nogap   drop(_cons)	stats(N r2_a) 
esttab su_5, nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nogap   drop(_cons)	stats(N r2_a) 

esttab su_3 using $Tables/AppendixC4.csv, nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nogap drop(_cons) stats(N r2_a) append
esttab su_5 using $Tables/Table9.csv	, nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nogap drop(_cons) stats(N r2_a) append
* *************************
!rm $Work/portf.dta 
!rm $Work/errors*.dta 
!rm $Work/tmp*.dta 
!rm $Work/SMB*.dta 
* *************************
* *************************
* THIS DEALS WITH FEs for LTG -- do not average across SMALL and BIG -- LTG sample is restricted to firms on the S&P
use datem RNK ege1 ege3 ege5 if dofm(datem)<mdy(1,1,2016) using $Work/XSection04, clear
reshape wide ege1 ege3 ege5, j(RNK) i(datem)

merge m:1 datem using /srv/Feb2023/TimeSeries01, keepusing(datem LTG) 
keep if _merge==3 & LTG<.
drop _merge

* High minus low
gen LMH5=ege51-ege510
gen LMH3=ege31-ege310

keep if dofm(datem)<mdy(1,1,2016)
foreach var of varlist  ege1* ege3* ege5* LMH3 LMH5 LTG  {
	egen tmp =std(`var')
	quietly: replace `var'=tmp
	drop tmp
}
tsset datem
gen L1LTG=L12.LTG
gen d1LTG=LTG-L1LTG
keep if d1LTG<.

eststo clear
quietly: eststo: ivreg2 ege31  d1LTG L1LTG , robust bw(37)
quietly: eststo: ivreg2 ege310 d1LTG L1LTG , robust bw(37)
quietly: eststo: ivreg2 LMH3   d1LTG L1LTG , robust bw(37)
esttab 					, unstack drop(_cons) nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nogaps scalar(r2_a)
esttab using $Tables/AppendixC2.csv	, unstack drop(_cons) nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nogaps scalar(r2_a) append
**********
eststo clear
quietly: eststo: ivreg2 ege51  d1LTG L1LTG , robust bw(61)
quietly: eststo: ivreg2 ege510 d1LTG L1LTG , robust bw(61)
quietly: eststo: ivreg2 LMH5   d1LTG L1LTG , robust bw(61)
esttab 					, unstack drop(_cons) nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nogaps scalar(r2_a)
esttab using $Tables/Table8.csv	, unstack drop(_cons) nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nogaps scalar(r2_a) replace
* *********************************************************
